<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>oop6(原型-常用开发模式)</title>
</head>
<body>
<script type="text/javascript">
	
	//原型的概念：原型对象里的所有属性和方法被所有构造函数实例化出来的对象所共享
	/*
	function Person () {}

	Person.prototype = {
		constructor: Person,
		name: 'z3',
		age: 25,
		friends: ['z6','z7'],
		getName: function () {
			alert(this.name);
		}
	}

	var p1 = new Person();
	var p2 = new Person();

	p1.friends.push('z8');
	
	//原型对象里的方法被所有对象所共享
	alert(p1.friends);
	alert(p2.friends);
	*/

	//组合使用构造函数和原型对象（常用开发方式）
	/*
	function Person (name, age, friends, job) {
		this.name = name;
		this.age = age;
		this.friends = friends;
		this.job = job;
	}

	Person.prototype = {
		constructor: Person,
		getName: function () {
			alert(this.name);
		}
	}

	var p3 = new Person('z3', 20, ['z4','z5'], '技术总监');
	var p4 = new Person('z4', 24, ['z2','z5','z8'], '项目经理');

	alert(p3.friends);
	p3.getName();
	alert(p4.friends);
	p4.getName();
	*/

	//动态原型模式（代码封装到一块）
	/*
	function Person (name, age, friends, job) {
		this.name = name;
		this.age = age;
		this.friends = friends;
		this.job = job;

		//动态原型方法
		if (typeof this.getName != 'function') {
			Person.prototype.getName = function () {
				alert(this.name);
			}
		}

	}

	var p5 = new Person('z4');
	
	p5.getName();
	*/

	//稳妥构造函数模式
	// 1 没有公共属性  2 不能使用this对象

	function Person (name) {
		var obj = new Object();

		//定义私有变量和方法 private
		var name = name;

		obj.getName = function () {
			alert(name);
		}

		return obj;
	}

	var p6 = new Person('w5');

	p6.getName();


</script>
</body>
</html>